package com.gxy.learn.design.model.create.singleton;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @description:
 * 用来测试实现序列化接口后再进行反序列化时会不会破坏单例
 * @author: gaoxueyong
 * @Date: 2022/11/6 16:57
 */
@Slf4j
public class ClientSerializable {
    public static void main(String[] args) throws Exception {
//        breakTest();
        test();
    }

    public static void test() throws Exception {
        writeFile();
        readFile();
        readFile();
//        两次读取的对象地址一样，表明序列化已破坏单例已被修复
//        19:56:23.015 [main] INFO com.gxy.learn.design.model.create.singleton.ClientSerializable - single = com.gxy.learn.design.model.create.singleton.SingletonSluggardStaticSafeSerializable@2cdf8d8a
//        19:56:23.018 [main] INFO com.gxy.learn.design.model.create.singleton.ClientSerializable - single = com.gxy.learn.design.model.create.singleton.SingletonSluggardStaticSafeSerializable@2cdf8d8a
    }

    public static void breakTest() throws Exception {
//        writeFileBreak();
//        两次读取的对象地址不一样，表面序列化已破坏单例
//        19:50:27.972 [main] INFO com.gxy.learn.design.model.create.singleton.ClientSerializable - single = com.gxy.learn.design.model.create.singleton.SingletonSluggardStaticSerializable@41975e01
//        19:50:27.975 [main] INFO com.gxy.learn.design.model.create.singleton.ClientSerializable - single = com.gxy.learn.design.model.create.singleton.SingletonSluggardStaticSerializable@5e91993f
//        readFileBreak();
//        readFileBreak();
    }

    public static void readFileBreak() throws Exception {
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("/Users/gaoxueyong/Desktop/a.txt"));
        SingletonSluggardStaticSerializable single = (SingletonSluggardStaticSerializable) inputStream.readObject();
        log.info("single = {}", single);
        inputStream.close();
    }

    public static void writeFileBreak() throws Exception {
        SingletonSluggardStaticSerializable single = SingletonSluggardStaticSerializable.getInstance();
//        创建输出对象
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("/Users/gaoxueyong/Desktop/a.txt"));
//        把Java对象写入文件
        outputStream.writeObject(single);
//        释放资源
        outputStream.close();
    }

    public static void readFile() throws Exception {
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("/Users/gaoxueyong/Desktop/b.txt"));
        SingletonSluggardStaticSafeSerializable single = (SingletonSluggardStaticSafeSerializable) inputStream.readObject();
        log.info("single = {}", single);
        inputStream.close();
    }

    public static void writeFile() throws Exception {
        SingletonSluggardStaticSafeSerializable single = SingletonSluggardStaticSafeSerializable.getInstance();
//        创建输出对象
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("/Users/gaoxueyong/Desktop/b.txt"));
//        把Java对象写入文件
        outputStream.writeObject(single);
//        释放资源
        outputStream.close();
    }
}
